// jquery datatable 对象
var searchTable;
var orgRoleData;
$(function(){
	$('#accountDictObj').blur(function(){checkAccountFun()});
	$('#dataPermissionTypeObj').change(function(){dataPermissionTypeChage(this);});
	$('#dataPermissionDiv').click(function(){showPermissionType();});
	
	// 后面构建btn 代码
	var btnModel = '    \
		{{#each func}}\
	    <button type="button" class="btn btn-{{this.type}} btn-sm" onclick="{{this.fn}}">{{this.name}}</button>\
	    {{/each}}';
	var template = Handlebars.compile(btnModel);
	// datatables 
	searchTable = $("#searchTable").DataTable({
		"ordering": false,// 排序
		"serverSide": true,// 开启服务器模式
		"scrollX": true,// 横向滚动
		ajax: {
            "type": "POST",
            "url":'search4Page.action',//请求路径
            "contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
            "dataType":'json',
            "data":function(d){// 查询参数
            	d.code = $('#sysDictCode').val();
            	d.name = $('#sysDictName').val();
            	return d;
            }
		},            
		columns: [// 对应列
		    {"data": "id"},
            {"data": "code"},
            {"data": "name"},
            {"data": "status"},
            {"data": "createDate"},
            {"data": "createUserName"},
            {"data": "lastUpdateDate"},
            {"data": "lastUpdateUserName"},
            {"data": null}
        ],
        "columnDefs": [
			{// 所有列默认值
				"targets": "_all",
				"defaultContent": ''
			},
			{// 隐藏第一列
				"targets": 0,
				"visible": false 
			},
			{
				targets: 3,
				render: function (a, b, c, d){
					if('Y' == c.status)return '有效';else return '无效';
				}
			},
			   {// 最后一列添加按钮
	            targets: -1,
	            render: function (a, b, c, d) {
	                var context =
	                {
	                    func: [
	                        {"name": "修改", "fn": "showEditPage(\'" + c.id + "\')", "type": "primary"},
	                        {"name": "删除", "fn": "del(\'" + c.id + "\',\'" + c.name + "\')", "type": "danger"},
	                    ]
	                }
	                ;
	                var html = template(context);
	                return html;
	            }
	        }]
			,"dom": 'rt<"bottom"ip><"clear">' //生成样式
		});
	});
// 查询
 function search4Page(){
	searchTable.ajax.reload();
}
 function showAddPage(){
	window.location.href = "sysDictAdd.action";
}
//update
 function showEditPage(id){
//	alert(id);
		/*$.ajax({
			url:"goUpdateSysdictAndSysdictitem.action",
			data:{id:id},
			dataType:"JSON",
			type:"POST"
		})*/
	 window.location.href = "goUpdateSysdictAndSysdictitem.action?id="+id;
}
//delect
function del(id,name){
	if(confirm('确定删除'+name+'?')){
		$.ajax({
			url:"delSysDictById.action",
			data:{id:id},
			dataType:"JSON",
			type:"POST",
			success:function(data){
				if(data){
					alert("删除成功！");
				}else{
					alert("删除失败！");
				}
				window.location.href = "selectSysDictById.action";
			},
			error:function(e){
				alert("删除字典信息时异常："+e);
			}
		})
	}
}
